package sisi.enquires;

import java.util.List;

import javax.persistence.EntityManager;

/**
 * ReqquoteController.java - The controller class for the <i>reqquote database table</i>. 
 * @author Pedro Gonzalez pedro@sisisoftware.it
 * @author Claudio Pelosi claudio@sisisoftware.it
 * @author SISI Software S.n.c. - www.sisisoftware.it
 * @version 1.0, 20/3/2014
 * 
 */
public class ReqquoteController{

	private EntityManager getEntityManager() {
		return new sisi.MyEmf().getEntityManager();
	}

	public Reqquote[] get(Integer idRequest) {
		List<Reqquote> lista = getList(idRequest);
			return lista.toArray(new Reqquote[0]);
		}

	@SuppressWarnings("unchecked")
	public List<Reqquote> getList(Integer idRequest) {

		String cWhere = "";
		cWhere = " WHERE c.reqid = "+idRequest;

		EntityManager em = getEntityManager();
		try {
			javax.persistence.Query q = em
				.createQuery("select c from Reqquote as c " + cWhere 
					+ " order by c.id ");

			List<Reqquote> lista = q.getResultList();
			return lista;
		} finally {
			em.close();
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<Reqquote> getListxIdReqEreqseq(Integer idRequest, Integer reqseq) {

		String cWhere = "";
		cWhere = " WHERE c.reqid = "+idRequest+" AND c.reqseq = "+reqseq;

		EntityManager em = getEntityManager();
		try {
			javax.persistence.Query q = em
				.createQuery("select c from Reqquote as c " + cWhere 
					+ " order by c.id ");

			List<Reqquote> lista = q.getResultList();
			return lista;
		} finally {
			em.close();
		}
	}

	public Reqquote getxId(int id) {

		EntityManager em = getEntityManager();
		try {
			Reqquote reqquotex = em.find(Reqquote.class, id);
			return reqquotex;
		} finally {
			em.close();
		}
	}

	@SuppressWarnings("finally")
	public Reqquote add(Reqquote reqquote) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			em.persist(reqquote);
			em.getTransaction().commit();
			reqquote = refresh(reqquote);
		} finally {
			em.close();
			return reqquote;
		}
	}

	@SuppressWarnings("finally")
	public boolean update(Reqquote reqquote) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Reqquote reqquotex = em.find(Reqquote.class, reqquote.getId());
			reqquotex.setId(reqquote.getId());
			reqquotex.setNote(reqquote.getNote());
			reqquotex.setSuppid(reqquote.getSuppid());
			reqquotex.setPortid(reqquote.getPortid());
			reqquotex.setSupname(reqquote.getSupname());
			reqquotex.setProdid(reqquote.getProdid());
			reqquotex.setProdname(reqquote.getProdname());
			reqquotex.setDt(reqquote.getDt());
			reqquotex.setReqid(reqquote.getReqid());
			reqquotex.setReqseq(reqquote.getReqseq());
			em.getTransaction().commit();
			em.flush();

		} finally {
			em.close();
		return false;
		}
	}

@SuppressWarnings("finally")
public Reqquote refresh(Reqquote reqquote) {

		Reqquote reqquotex = reqquote;
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			reqquotex = em.find(Reqquote.class, reqquote.getId());
			em.flush();
			em.refresh(reqquotex);
		} finally {
			em.close();
			return reqquotex;
		}
}

@SuppressWarnings("finally")
public boolean remove(Reqquote reqquote) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Reqquote reqquotex = em.find(Reqquote.class, reqquote.getId());
			em.remove(reqquotex);
			em.getTransaction().commit();
		} finally {
			em.close();
			return false;
		}
}

}
